<?php

//小站选项应用模型Album模块
class Album_model extends Model {
	
	var $viewdata;

	function Album_model()
	{
		parent::Model();	
		$this->load->database();
		$this->load->model("plugins_model", "pm");
	}
	
	//获取列表
	function listitem($offset = 0, $pagecount = 20, $order = "id", $desc = "desc")
	{
		$items = array();
		$query = $this->db->from("module_album_info")->limit($pagecount, $offset)->order_by($order, $desc)->get();
		if ($query->num_rows()>0) {
			$items = $query->result_array();
			foreach($items as $k => $list){
				$siteid = $this->pm->get_siteid($list["plid"]);
				$site = $this->sm->get($siteid);
				if($site) {
					$items[$k]["domain"] = $site["domain"];
					$items[$k]["sitename"] = $site["name"];
					$items[$k]["logo"] = $site["logo"];
				} else {
					$items[$k]["domain"] = $items[$k]["sitename"] = $items[$k]["logo"] = "";
				}
				
				//用户信息
				$aid = $list["aid"];
				$q = $this->db->get_where("module_album", array("id"=>$aid));
				$row = $q->row_array();
				$userid = $row["userid"];
				$userinfo = $this->am->get($userid);
				$items[$k]["userinfo"] = $userinfo;
			}
		}
		return $items;
	}
	
	//计算总数
	function countitems()
	{
		return $this->db->count_all_results("module_album_info");
	}
	
	//添加
	function add($plid)
	{
		//基本信息
		$data = array(
			"userid" => $this->userid,
			"plid" => $plid,
			"addtime" => time(),
		);
		$result = $this->db->insert("module_album", $data);
		$insert_id = $this->db->insert_id();

		//上传图片处理
		$picnum = 0;
		$pics = array();
		$this->load->library("imagecrop");//加载library
		for ($i=1; $i<=5; $i++) {
			$pic = ciupload("pic".$i);
			if ($pic != "") {

				$params =array('src_file' => './data/upload_pic/'.$pic, 'dst_file' => './data/upload_pic/'.$pic);
				$this->imagecrop->src_file=$params['src_file'];
				$this->imagecrop->dst_file=$params['dst_file'];
				$this->imagecrop->LoadImage();
				$this->imagecrop->Crop(560,420,4);
				$this->imagecrop->SaveImage();
				$this->imagecrop->destory();
			
				$data = array(
					"aid" => $insert_id,
					"plid" => $plid,
					"pic" => $pic,
					"addtime" => time(),
				);
				$this->db->insert("module_album_info", $data);
				$_insert_id = $this->db->insert_id();
				$pics[] = array_merge($data, array("id"=>$_insert_id));
				$picnum++;
			}
		}
		$insert_id = "multi_".$insert_id;
		//跳转到编辑
		show_success("添加成功", '/site/'.$this->site['domain'].'/plugin/edit/'.$plid.'/'.$insert_id);
	}
	
	//编辑
	function edit($plid)
	{
		//获取编辑ID
		$aid = p("aid");
		$query = $this->db->get_where("module_album_info", array("aid"=>$aid));
		if ($query->num_rows()>0) {
			$result = $query->result_array();
			//执行批量更新
			foreach ($result as $row) {
				if (p("content".$row['id']) != "") {
					$data = array(
						"content" => p("content".$row['id']),
					);
					$this->db->where("id", $row['id']);
					$this->db->update("module_album_info", $data);
				}
//				}				
			}
			//添加新鲜事(只添加第一条记录)
			$row = $result[0];
			$public = ($this->userid != $this->site['userid']) ? 1 : 0;
			$data = array(
			'title' => p("content".$row['id']),
			'userid' => $this->userid,
			'username' => $this->userinfo['username'],
			'userphoto' => $this->userinfo['photo'],
			'siteid' => $this->site['id'],
			'sitename' => $this->site['name'],
			'sitelogo' => $this->site['info']['logo'],
			'action' => 'update',
			'link' => '/site/'.$this->site['domain'].'/show/'.$this->moduleid.'/'.$row['id'],
			'addtime' => time(),
			'public' => $public,
			);
			$this->autoload_model->add_fresh($this->moduleid, $data);
		}
		return true;
	}
	
	//删除
	function delete($dataid)
	{
		$this->db->delete("module_album", array("id"=>$dataid));
		$result = $this->db->delete("module_album_info", array("aid"=>$dataid));
		return $result;
	}
	
	//删除
	function deleteinfo($dataid)
	{
		$result = $this->db->delete("module_album_info", array("id"=>$dataid));
		return $result;
	}
	
	//删除选择
	function delselected($delstr)
	{
		$delid = array();
		$_delid = explode("_", $delstr);
		//数组处理
		for ($i=0; $i<count($_delid); $i++) {
			if(intval($_delid[$i]) >0) {
				$delid[]= $_delid[$i];
			}
		}
		if(count($delid) == 0) return false;
		//删除评论
//		$this->comment_model->forum_delete($id);
		$this->db->where_in("dataid", $delid);
		$this->db->delete("comment");

		$this->db->where_in("id", $delid);
		$result = $this->db->delete("module_album_info");
		return $result;
	}
	
	//#获取列表
	function lists($plid, $limit = 16, $offset = 0)
	{
		$lists = array();
		$query = $this->db->from("module_album_info")->where(array("plid"=>$plid))->order_by("id","desc")->limit($limit, $offset)->get();
		if($query->num_rows()>0) {
			$lists = $query->result_array();
			foreach ($lists as $k => $row) {
				//模块ID
				$plid = $row["plid"];
//				$q = $this->db->get_where("site_plugins", array("id"=>$plid));
//				$r = $q->row_array();
				$r = $this->pm->get($plid);
				$lists[$k]["moduleid"] = $r["moduleid"];
			}
		}
		return $lists;
	}
	
	//计算总数
	function count_lists($plid)
	{
		$query = $this->db->from("module_album_info")->where(array("plid"=>$plid))->get();
		return $query->num_rows();
	}
	
	//获取信息
	function get_info($dataid)
	{
		$result = array();
		if(!strpos($dataid, 'ti_')){
			$query = $this->db->get_where("module_album_info", array("id"=>$dataid));
		} else {
			$dataid = str_replace("multi_", "", $dataid);
			$query = $this->db->get_where("module_album_info", array("aid"=>$dataid));
		}
		if($query->num_rows()>0) {
			$result = $query->result_array();
			foreach ($result as $key => $row) {
				$q = $this->db->get_where("module_album", array("id"=>$row['aid']));
				$r = $q->row_array();
				$result[$key]['userid'] = $r['userid'];
			}
		}
		return $result;
	}
	
	//#查看内容
	function show($id)
	{
		$row = array();
		$query = $this->db->get_where("module_album_info", array("id"=>$id));
		if($query->num_rows()>0) {
			$row = $query->row_array();
			//获取userid
			$q = $this->db->get_where("module_album", array("id"=>$row['aid']));
			$r = $q->row_array();
			$row["userid"] = $r["userid"];
			$row["userinfo"] = $this->am->get($r['userid']);
			
			//模块ID
			$plid = $row["plid"];
//			$q = $this->db->get_where("site_plugins", array("id"=>$plid));
//			$r = $q->row_array();
			$r = $this->pm->get($plid);
			if(empty($r)) show_404();
			$row["moduleid"] = $r["moduleid"];
			
			//获取上一张与下一张ID
			$row["previd"] = $this->show_previd($plid, $row['id']);
			$row["nextid"] = $this->show_nextid($plid, $row['id']);
			
			//小站信息
			$r = $this->pm->get($row["plid"]);
			$opid = $r["opid"];
			$r = $this->options_model->get($opid);
			if ($r) {
				$siteid = $r["siteid"];
				$row["siteid"] = $siteid;
				//小站信息
				$row["site"] = $this->sm->get_site($siteid);
			} else {
				$row["site"]["name"] = '';
			}
		}
		return $row;
	}
	
	//获取下一张ID
	function show_nextid($plid, $id) 
	{
		$nextid = $id;
		$query = $this->db->from("module_album_info")->where(array("plid"=>$plid,"id <"=>$id))->order_by("id","desc")->limit(1)->get();
		if ($query->num_rows()>0) {
			$row = $query->row_array();
			$nextid = $row['id'];
		}
		return $nextid;
	}
	
	//获取上一张ID
	function show_previd($plid, $id) 
	{
		$previd = $id;
		$query = $this->db->from("module_album_info")->where(array("plid"=>$plid,"id >"=>$id))->order_by("id","asc")->limit(1)->get();
		if ($query->num_rows()>0) {
			$row = $query->row_array();
			$previd = $row['id'];
		}
		return $previd;
	}
	
}

/* End of file */